﻿//
// GNS_ApplyTopUp.cpp
// MobileManageSys
//FuncTion 申请充值
// Created by mr.l on 16/1/14.
//
//
#include "GnsClientFrame.h"
#include "GNS_ApplyTopUp.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "GNS_Common.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
////extern ISocketMgr * _GameSock;

GNS_ApplyTopUp::GNS_ApplyTopUp()
:_TopID(nullptr)
, _TopUplines(nullptr)
{
    pGNS_ApplyTopUp=0;
    TopID=nullptr;
    NoteTheName=nullptr;
    TopUplines=nullptr;
    RLines=nullptr;
    NoteManage=nullptr;
    pPopWnd=nullptr;
}

GNS_ApplyTopUp::~GNS_ApplyTopUp()
{
    TopID=nullptr;
    NoteTheName=nullptr;
    TopUplines=nullptr;
    RLines=nullptr;
    NoteManage=nullptr;
    pPopWnd=nullptr;
}

GNS_ApplyTopUp* GNS_ApplyTopUp::createLayer()
{
    auto pApplyTopUp = GNS_ApplyTopUp::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pApplyTopUp);
    return pApplyTopUp;
}

bool GNS_ApplyTopUp::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_ApplyTopUp::SetNodeName(std::string CsbName)
{
    pGNS_ApplyTopUp = CSLoader::createNode(CsbName);
    pGNS_ApplyTopUp->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_ApplyTopUp);
    addChild(pGNS_ApplyTopUp,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_ApplyTopUp);
    auto pBackBtn = static_cast<Button*>(pGNS_ApplyTopUp->getChildByName("ApplyTopBackBtn"));
    pBackBtn->setTag(10);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_ApplyTopUp::BtnCallBack, this));
    
    TopID = static_cast<TextField*>(pGNS_ApplyTopUp->getChildByName("TopUpText"));
    //((TextField*)TopID)->addEventListener(CC_CALLBACK_2(GNS_ApplyTopUp::textFieldEvent, this,1));
	_TopID = GNS_Handler::createTMEditBoxWithExistFeild(TopID, pGNS_ApplyTopUp, 14, EditBox::InputMode::NUMERIC);
	_TopID->addEditingEndCallback(CC_CALLBACK_0(GNS_ApplyTopUp::textFieldIDEndCallback,this));
    
   NoteTheName = static_cast<Text*>(pGNS_ApplyTopUp->getChildByName("NoteTheName"));
   // pGNS_ApplyTopUp->addChild(NoteTheName,12);
    TopUplines = static_cast<TextField*>(pGNS_ApplyTopUp->getChildByName("Toplines"));
	_TopUplines = GNS_Handler::createTMEditBoxWithExistFeild(TopUplines, pGNS_ApplyTopUp, 10, EditBox::InputMode::NUMERIC);
	_TopUplines->addEditingEndCallback(CC_CALLBACK_0(GNS_ApplyTopUp::textFieldGoldEndCallback, this));
    //((TextField*)TopUplines)->addEventListener(CC_CALLBACK_2(GNS_ApplyTopUp::textFieldEvent, this,2));
    
     RLines = static_cast<Text*>(pGNS_ApplyTopUp->getChildByName("Text_2"));
    NoteManage = static_cast<TextField*>(pGNS_ApplyTopUp->getChildByName("NoteManage"));
    
    auto TopSentDate = static_cast<Button*>(pGNS_ApplyTopUp->getChildByName("TopSendbtn"));
    TopSentDate->setTag(1);
    TopSentDate->addTouchEventListener(CC_CALLBACK_2(GNS_ApplyTopUp::BtnCallBack, this));
   // TopSentDate->addTouchEventListener(const ccWidgetTouchCallback &callback)
    
}
void GNS_ApplyTopUp::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 10:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                     this->getParent()->removeChild(this);
                }
                //auto SystemNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(0);
                //SystemNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("SystemMgr", true);
                break;
            }
            case 1:
            {
                SenDetection();
                break;
            }
            default:
                break;
        }
    }
}
void GNS_ApplyTopUp::SenDetection()
{
	string topid = _TopID->getText();
	string gold = _TopUplines->getText();
	string remark = NoteManage->getString();
	if (!CCGnsOften::isNumber(topid))
	{
		OnUIAction("充值对象错误！", true);
	}
	else if (gold == "0" || !CCGnsOften::isNumber(gold))
	{
		OnUIAction("充值金额错误！", true);
	}
	else if (!CCGnsOften::checkBeiZhu(remark))
	{
		OnUIAction("备注过长！", true);
	}
	else
	{
		CMD_CSG_AppToRechageOrExchange pAppToReOrEx;
		pAppToReOrEx._sendAgentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
		pAppToReOrEx._revAgentCode = atoi(_TopID->getText());
		pAppToReOrEx._rechangeGold = atoi(_TopUplines->getText());
		pAppToReOrEx._operType = 1;
		string remarkTmp = SystemMgr::getInstance()->utf8ToGb(NoteManage->getString().c_str());
		memcpy(pAppToReOrEx._remark, remarkTmp.c_str(), remarkTmp.length() + 1);
		////_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CSG_REQUEST_EXCHANGE_RMB_TO_GOLD, &pAppToReOrEx, sizeof(pAppToReOrEx));
		GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CSG_REQUEST_EXCHANGE_RMB_TO_GOLD, (char*)&pAppToReOrEx, sizeof(pAppToReOrEx));

	}
}

void GNS_ApplyTopUp::textFieldGoldEndCallback()
{
	std::string TopUplin = _TopUplines->getText();
	std::string topid = _TopID->getText();
	int level = GNS_PlayerMgr::getInstance()->getUserLevel();
	int id;
	if (level == 0)
		id = atoi(topid.c_str());
	else
		id = 0;
	int proTopline = 0;
	bool IsSec = false;
	for (auto it : *(GNS_PlayerMgr::getInstance()->getAgRation())) {
		if (it._lowlineCode == id) {
			proTopline = it._lowlineRechargeRatio;
			IsSec = true;
			break;
		}
	}
	if (IsSec == false) {
		proTopline = GNS_PlayerMgr::getInstance()->getPlayTop();
	}

	int RlinesNum = (int)(atoi(TopUplin.c_str()) / proTopline);
	RLines->setString(StringUtils::format("%d", RlinesNum));
	/*if (topid.length() != 0) {

	}*/
	//else{
	//	auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
	//	pPopWnd->setClickWndOutsideClose(true);
	//	pPopWnd->SetTitleWithUTF("提示!");
	//	pPopWnd->SetContentTextWithUTF("请输入玩家账号", Color3B::BLACK);
	//	// pPopWnd->PopWndEffectseffects("RunCircle.plist",true);
	//}
}

void GNS_ApplyTopUp::textFieldIDEndCallback()
{
	std::string InputStr = SystemMgr::getInstance()->utf8ToGb(_TopID->getText());
	std::map<int, string>::iterator it = GNS_PlayerMgr::getInstance()->getRemarkName()->find(atoi(InputStr.c_str()));
	if (it != GNS_PlayerMgr::getInstance()->getRemarkName()->end() && it->second != "")
	{
		NoteTheName->setString(SystemMgr::getInstance()->GbToUTF8(it->second.c_str()));
	}
	else
	{
		int level = GNS_PlayerMgr::getInstance()->getUserLevel();
		if (level == 0)
		{
			NoteTheName->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("无"));
		}
		else
		{
			NoteTheName->setString(_TopID->getText());
		}
	}
}

void GNS_ApplyTopUp::OnUIAction(std::string Text, bool isUTF)
{
	pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
	pPopWnd->setClickWndOutsideClose(true);
	pPopWnd->SetTitleWithUTF("提示!");
	if (isUTF)
		pPopWnd->SetContentTextWithUTF(Text, Color3B::BLACK);
	else
		pPopWnd->SetContentTextWithGB(Text, Color3B::BLACK);
}


//void GNS_ApplyTopUp::textFieldEvent(cocos2d::Ref *pSender, ui::TextField::EventType type, int flag)
//{
//
//	//auto pTextField = (TextField*)(pSender);
//
//	switch (type) {
//	case TextField::EventType::ATTACH_WITH_IME:
//	{
//												  log("1");
//												  break;
//	}
//	case TextField::EventType::DETACH_WITH_IME:
//	{
//
//												  //  if (pTextField==TopID) {
//												  if (flag == 1){
//													  std::string InputStr = SystemMgr::getInstance()->utf8ToGb(TopID->getString().c_str());
//													  std::map<int, string>::iterator it = GNS_PlayerMgr::getInstance()->getRemarkName()->find(atoi(InputStr.c_str()));
//													  if (it != GNS_PlayerMgr::getInstance()->getRemarkName()->end() && it->second != "")
//													  {
//														  NoteTheName->setString(SystemMgr::getInstance()->GbToUTF8(it->second.c_str()));
//													  }
//													  else
//													  {
//														  int level = GNS_PlayerMgr::getInstance()->getUserLevel();
//														  if (level == 0)
//														  {
//															  NoteTheName->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("无"));
//														  }
//														  else
//														  {
//															  NoteTheName->setString(TopID->getString());
//														  }
//													  }
//												  }
//												  else if (flag == 2){  //TopUplines
//													  std::string TopUplin = TopUplines->getString();
//													  if (TopID->getString().length() != 0) {
//														  int level = GNS_PlayerMgr::getInstance()->getUserLevel();
//														  int id;
//														  if (level == 0)
//															  id = atoi(TopID->getString().c_str());
//														  else
//															  id = 0;
//														  int proTopline = 0;
//														  bool IsSec = false;
//														  for (auto it : *(GNS_PlayerMgr::getInstance()->getAgRation())) {
//															  if (it._lowlineCode == id) {
//																  proTopline = it._lowlineRechargeRatio;
//																  IsSec = true;
//																  break;
//															  }
//														  }
//														  if (IsSec == false) {
//															  proTopline = GNS_PlayerMgr::getInstance()->getPlayTop();
//														  }
//
//														  int RlinesNum = (int)(atoi(TopUplin.c_str()) / proTopline);
//														  RLines->setString(StringUtils::format("%d", RlinesNum));
//													  }
//													  else{
//														  auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
//														  pPopWnd->setClickWndOutsideClose(true);
//														  pPopWnd->SetTitleWithUTF("提示!");
//														  pPopWnd->SetContentTextWithUTF("请输入玩家账号", Color3B::BLACK);
//														  // pPopWnd->PopWndEffectseffects("RunCircle.plist",true);
//													  }
//
//
//												  }
//
//												  break;
//	}
//	case TextField::EventType::INSERT_TEXT:
//	{
//											  log("3");
//											  break;
//	}
//	case TextField::EventType::DELETE_BACKWARD:
//	{
//												  log("4");
//												  break;
//	}
//	default:
//		break;
//	}
//	//   */
//}